

main(){
  //类的作用：封装数据和方法，用于创建对象
  // Dart中可以省略关键字 new
  Person p = new Person('张三',18,'男');
  print(p.name);
  print(p.age);
  print(p.sex);
  p.say();
  // 调用命名构造函数
  Person p2 = new Person.now(name: '李四', age: 19);
  p2.say();
  print(p2.age);
  print(p2.sex);
  print(p2.name);

}
class Person{
  String? name;
  int? age;
  String? sex;
  // 主构造函数
  Person(String? name,int? age,String? sex){
    this.name = name;
    this.age = age;
    this.sex = sex;
  }

  //命名构造函数
  Person.now({String? name,int? age}){
    this.name = name;
    this.age = age;
    this.sex = '男';
    print('命名构造函数');
  }

//方法
  void say(){
    print('我是$name,我今年$age岁,我是$sex');
  }

}
